//
//  SC_CurveChartView.m
//  SCCharts
//
//  Created by Yang on 9/2/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "SC_CurveChartView.h"
#import "SC_PointData.h"
#import "UIView+SCCategory.h"

#pragma mark 图表缩进
static float ChartTopMargin = 14.0f;
static float ChartBottomMargin = 30.0f;
static float ChartLeftMargin = 35.0f;
static float ChartRightMargin = 20.0f;


#pragma mark 与刻度有关的数据
static float DataHighValue = 0.0f;
static float YAxisLineLength = 0.0f;
static float XAxisLineLength = 8.0f;
static float AxisLabelFontSize = 14.0f;
static float XAxisLabelTextMaxLength = 0.0f;  

#pragma mark 与化曲线有关的数据
static float CurvePointDistance = 0.0f;


#pragma mark IndicatorView
@interface SC_CurveIndicatorView : UIView {
	bool needShowIndicator;
	
	NSMutableString *indicatorValue;
	NSMutableString *indicatorXLabel;
	CGPoint indicatorPoint;
	
}
@property(nonatomic,assign)bool needShowIndicator;
@property(nonatomic,retain)NSMutableString *indicatorValue;
@property(nonatomic,retain)NSMutableString *indicatorXLabel;
@property(nonatomic,assign)CGPoint indicatorPoint;

- (void)drawIndicator:(CGContextRef )context;
- (void)reset;
- (void)resetFrame:(CGRect )rect;

@end

@implementation SC_CurveIndicatorView
@synthesize needShowIndicator,indicatorPoint,indicatorValue,indicatorXLabel;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
		needShowIndicator = NO;
		indicatorPoint = CGPointMake(0.0f, 0.0f);
		indicatorXLabel = [[NSMutableString alloc] init];
		indicatorValue = [[NSMutableString alloc] init];
    }
    return self;
}
- (void)reset {
	needShowIndicator = NO;
	[self setNeedsDisplay];
}
- (void)resetFrame:(CGRect )rect {
	self.frame = rect;
	[self reset];
}
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
	 CGContextRef context = UIGraphicsGetCurrentContext();
	 if (needShowIndicator) {
		 [self drawIndicator:context];
	 }
 }
- (void)drawIndicator:(CGContextRef )context {
	float height = self.frame.size.height;
	CGContextSetStrokeColorWithColor(context,[UIColor greenColor].CGColor);
	CGContextSetLineWidth(context, 1.0);
	CGPoint addLines[] =
	{
		CGPointMake(indicatorPoint.x, ChartTopMargin),
		CGPointMake(indicatorPoint.x, height - ChartBottomMargin),
	};
	CGContextAddLines(context, addLines, sizeof(addLines)/sizeof(addLines[0]));
	CGContextStrokePath(context);
	
	CGContextSetFillColorWithColor(context,[UIColor orangeColor].CGColor);
	
	CGContextSetLineWidth(context, 3);
	CGContextFillEllipseInRect(context, CGRectMake(indicatorPoint.x-6, indicatorPoint.y-6, 12.0, 12.0));
	
	CGSize size = [indicatorValue sizeWithFont:[UIFont systemFontOfSize:12.0f]];
	if (size.height < 15.0f) {
		size.height = 15.0f;
	}
	if (size.width < 40.0f) {
		size.width = 40.0f;
	}
	CGRect rect;
	rect.origin.x = indicatorPoint.x - size.width / 2;
	rect.origin.y = ChartTopMargin - size.height;
	rect.size = size;
	
//// 修改
	
	//[UIView drawRoundRectangleInRect:rect withRadius:4.0 color:[UIColor grayColor]];
	CGFloat radius = 4.0f;
	
	//CGContextRef context = UIGraphicsGetCurrentContext();
	[[UIColor grayColor] set];
	
	CGRect rrect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height );
	
	CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect);
	CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect);
	CGContextMoveToPoint(context, minx, midy);
	CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
	CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
	CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
	CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
	CGContextClosePath(context);
	CGContextDrawPath(context, kCGPathFill);
	
/// 修改结束///
	
	[[UIColor whiteColor] set];
	[indicatorValue drawInRect:rect withFont:[UIFont boldSystemFontOfSize:12.0f] lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
	[[UIColor grayColor] set];
	
	CGSize labelSize = [indicatorXLabel sizeWithFont:[UIFont systemFontOfSize:12.0f]];
	
	float margin = 0.0f;
	if (indicatorPoint.x > self.frame.size.width / 2) {
		margin = labelSize.width;
	}
	[indicatorXLabel drawAtPoint:CGPointMake(indicatorPoint.x - margin, ChartTopMargin)//self.frame.size.height - ChartBottomMargin - labelSize.height)
						withFont:[UIFont systemFontOfSize:12.0f]];
}

- (void)dealloc {
	[indicatorValue release];
	[indicatorXLabel release];
    [super dealloc];
}
@end

#pragma mark SC_CurveChartView

@implementation SC_CurveChartView

@synthesize indicatorView;

#pragma mark Reset Chart Margin Methods
- (void)setTopMargin:(float )margin {
	ChartTopMargin = margin;
	[self setNeedsDisplay];
}
- (void)setBottomMargin:(float )margin {
	ChartBottomMargin = margin;
	[self setNeedsDisplay];
}
- (void)setLeftMargin:(float )margin {
	ChartLeftMargin = margin;
	[self setNeedsDisplay];
}
- (void)setRightMargin:(float )margin {
	ChartRightMargin = margin;
	[self setNeedsDisplay];
}

#pragma mark init View Methods

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        self.backgroundColor = [UIColor whiteColor];
		dataArray = [[NSMutableArray alloc] init];
//		needShowIndicator = NO;
		needShowTitle = NO;
//		indicatorPoint = CGPointMake(0.0f, 0.0f);
//		indicatorXLabel = [[NSMutableString alloc] init];
//		indicatorValue = [[NSMutableString alloc] init];
		
		indicatorView = [[SC_CurveIndicatorView alloc] initWithFrame:self.bounds];
		indicatorView.backgroundColor = [UIColor clearColor];
		indicatorView.userInteractionEnabled = NO;
		[self addSubview:indicatorView];
		
		standardValue = - 999.0f;
		lowLimitedValue = - 999.0f;
    }
    return self;
}

//- (void)setNeedsDisplay {
//	CGRect rect = self.bounds;
//	[indicatorView resetFrame:rect];
//	[super setNeedsDisplay];
//}

- (void)setTitle:(NSString *)string {
	title = string;
}
- (void)needShowTitle:(NSString *)string isShow:(bool)show {
	needShowTitle = show;
	title = string;
}
- (void)setData:(NSArray *)array {
	[dataArray setArray:array];
	[self calculateHighValue];
	[self setNeedsDisplay];
}
- (void)calculateHighValue {
	DataHighValue = 0.0f;
	XAxisLabelTextMaxLength = 0.0f;
	for (SC_PointData *point in dataArray){
		if ([point.yValue floatValue] > DataHighValue) {
			DataHighValue = [point.yValue floatValue];
		}
		
		NSString *string;
		if (point.xLabel.length == 16) {
			string = [point.xLabel substringFromIndex:11];
		}
		else {
			string = point.xLabel;
		}
		CGSize size = [string sizeWithFont:[UIFont systemFontOfSize:AxisLabelFontSize]];
		if (size.width > XAxisLabelTextMaxLength) {
			XAxisLabelTextMaxLength = size.width;
		}
	}
	
	
	if (standardValue != -999.0f && standardValue > DataHighValue) {
		DataHighValue = standardValue;
	}
	
	DataHighValue *= 1.2;
	
	//NSLog(@"DataHighValue :%.0f",DataHighValue);
//	int temp = DataHighValue;
//	if (temp%3 != 0) {
//		int x = DataHighValue / 3;
//		DataHighValue = (x + 1) * 3;
//	}
	if (DataHighValue < 10.0f) {
//		if (temp%3 != 0) {
//			int x = DataHighValue / 3;
//			DataHighValue = (x + 1) * 3;
//		}
	}
	else {
		int temp = DataHighValue;
		if (temp % 30 != 0) {
			int x = DataHighValue / 30;
			DataHighValue = (x + 1)* 10 * 3;
		}
	}


}
#pragma mark Drawing Methods

- (void)drawRect:(CGRect)rect {
	CGRect rectTemp = self.bounds;
	[indicatorView resetFrame:rectTemp];
	

	CGContextRef context = UIGraphicsGetCurrentContext();
	[self drawOutLineFrameBox:context]; // 画外框
	if ([dataArray count] < 1) {
		// 当无可显示的数据时，显示以下文字
		[[UIColor lightGrayColor] set];
		NSString *string = @"无可显示的数据";
		CGSize size = [string sizeWithFont:[UIFont systemFontOfSize:17.0f]];\
		CGSize chartSize = self.bounds.size;
		CGPoint point = CGPointMake(ChartLeftMargin + (chartSize.width - ChartLeftMargin - ChartRightMargin) / 2.0f - size.width / 2.0f,
									ChartTopMargin + (chartSize.height - ChartTopMargin - ChartBottomMargin) / 2.0f - size.height / 2.0f);
		[string drawAtPoint:point withFont:[UIFont systemFontOfSize:17.0f]];
		return;
	}
	[self drawYAxisLabels:context]; // 画纵坐标刻度
	[self drawXAxisLabels:context]; // 画横坐标刻度
	[self drawCurveFill:context];   // 画填充图层
	[self drawCurveLines:context]; // 画曲线
	if (standardValue != - 999.0f) {
		[self drawStandardLine:context]; // 画标准线
	}
	if (lowLimitedValue != - 999.0f) {
		[self drawLowLimitedLine:context]; // 下限线
	}
//	if (needShowIndicator) {
//		[self drawIndicator:context]; // 触摸曲线后触发坐标线
//	}
	if (needShowTitle) {
		[self drawTitleText:context];
	}
	
}
- (void)drawOutLineFrameBox:(CGContextRef)context {
	float height = self.frame.size.height;
	float width = self.frame.size.width;
	CGContextSetAllowsAntialiasing(context, NO);
	CGContextSetStrokeColorWithColor(context,[UIColor grayColor].CGColor);
	CGContextSetLineWidth(context, 1.0);
	CGPoint addLines[] =
	{
		CGPointMake(ChartLeftMargin, ChartTopMargin),
		CGPointMake(width - ChartRightMargin, ChartTopMargin),
		CGPointMake(width - ChartRightMargin, height - ChartBottomMargin),
		CGPointMake(ChartLeftMargin, height - ChartBottomMargin),
		CGPointMake(ChartLeftMargin, ChartTopMargin)
	};
	CGContextAddLines(context, addLines, sizeof(addLines)/sizeof(addLines[0]));
	CGContextStrokePath(context);
	CGContextSetAllowsAntialiasing(context, YES);
}
- (void)drawYAxisLabels:(CGContextRef)context {
	
	float step = (self.frame.size.height - ChartTopMargin - ChartBottomMargin) / 3.0f;
	CGContextSetAllowsAntialiasing(context, NO);
	CGContextSetStrokeColorWithColor(context,[UIColor grayColor].CGColor);
	CGContextSetLineWidth(context, 1.0);
	CGPoint strokeSegments[] =
	{
		CGPointMake(ChartLeftMargin - YAxisLineLength, ChartTopMargin),
		CGPointMake(ChartLeftMargin, ChartTopMargin),
		CGPointMake(ChartLeftMargin - YAxisLineLength, ChartTopMargin + step),
		CGPointMake(ChartLeftMargin, ChartTopMargin + step),
		CGPointMake(ChartLeftMargin - YAxisLineLength, ChartTopMargin + step * 2),
		CGPointMake(ChartLeftMargin, ChartTopMargin + step * 2),
		CGPointMake(ChartLeftMargin - YAxisLineLength, ChartTopMargin + step * 3),
		CGPointMake(ChartLeftMargin, ChartTopMargin + step * 3),
	};
	CGContextStrokeLineSegments(context, strokeSegments, sizeof(strokeSegments)/sizeof(strokeSegments[0]));
	CGContextStrokePath(context);
	CGContextSetAllowsAntialiasing(context, YES);
	
	if (DataHighValue != 0.0f) {
		float margin = DataHighValue / 3.0f;
		//NSLog(@"margin :%d",margin);
		
		for (int i = 0; i < 4; i ++) {
			
//			if (standardValue != -999.0f && (DataHighValue - i *margin) > standardValue) {
//				[[UIColor redColor] set];
//			}
//			else {
//				[[UIColor blackColor] set];
//			}
			// 只显示最高的和最低的
			if (i > 0 && i < 3) {
				continue;
			}

			
			NSString *string;
			if (DataHighValue < 1.0f) {
				string = [NSString stringWithFormat:@"%.2f", DataHighValue - i * margin];
			}
			else if (DataHighValue < 20.0f) {
				string = [NSString stringWithFormat:@"%.1f", DataHighValue - i * margin];
			}
			else {
				string = [NSString stringWithFormat:@"%.0f", DataHighValue - i * margin];
			}

			CGSize size = [string sizeWithFont:[UIFont systemFontOfSize:AxisLabelFontSize]];
			CGPoint point = CGPointMake(ChartLeftMargin - size.width - YAxisLineLength - 5.0f, ChartTopMargin + i * step - size.height/2);
			[string drawAtPoint:point withFont:[UIFont systemFontOfSize:AxisLabelFontSize]];
		}
	}
}
- (void)drawXAxisLabels:(CGContextRef)context {
	if ([dataArray count] < 1) {
		return;
	}
	NSInteger dataCount = [dataArray count];
	CurvePointDistance = (self.frame.size.width - ChartLeftMargin - ChartRightMargin)/(float)(dataCount - 1);
	//CGContextSetAllowsAntialiasing(context, NO);
	NSInteger countMargin;
	if (CurvePointDistance < 1.0f) {
		countMargin = (XAxisLabelTextMaxLength + 10.0f) / CurvePointDistance;
	}
	else if ((int)(XAxisLabelTextMaxLength + 10.0f) % (int)CurvePointDistance == 0) {
		countMargin = (XAxisLabelTextMaxLength + 10.0f) / CurvePointDistance;
	}
	else {
		countMargin = (XAxisLabelTextMaxLength + 10.0f) / CurvePointDistance + 1; 
	}
	for (int i = 0; i < dataCount; i ++) {
		if (i % countMargin == 0) {
			CGContextSetAllowsAntialiasing(context, NO);
			CGContextSetStrokeColorWithColor(context,[UIColor grayColor].CGColor);
			CGContextSetLineWidth(context, 1.0);
			CGPoint strokeSegments[] =
			{
				CGPointMake(ChartLeftMargin + CurvePointDistance * i, self.frame.size.height - ChartBottomMargin),
				CGPointMake(ChartLeftMargin + CurvePointDistance * i, self.frame.size.height - ChartBottomMargin + XAxisLineLength),
			};
			CGContextStrokeLineSegments(context, strokeSegments, sizeof(strokeSegments)/sizeof(strokeSegments[0]));
			CGContextStrokePath(context);
			CGContextSetAllowsAntialiasing(context, YES);
			
			SC_PointData *point = [dataArray objectAtIndex:i];
			
			NSString *string;
			if (point.xLabel.length == 16) {
				string = [point.xLabel substringFromIndex:11];
			}
			else {
				string = point.xLabel;
			}
			
			CGSize size = [string sizeWithFont:[UIFont systemFontOfSize:AxisLabelFontSize]];
			[string drawAtPoint:CGPointMake(ChartLeftMargin + CurvePointDistance * i - size.width/2,
												  self.frame.size.height - ChartBottomMargin + XAxisLineLength) //+ size.height/2 + 5.0f)
							 withFont:[UIFont systemFontOfSize:AxisLabelFontSize]];
		}
	}
}
- (void)drawCurveLines:(CGContextRef )context {
	CGContextSaveGState(context);
	CGContextSetRGBStrokeColor(context, 68.0/255.0, 152.0/255.0, 211.0/255.0, 1.0);
	CGContextSetLineWidth(context, 3);
	CGContextSetLineCap(context, kCGLineCapRound);
	CGContextSetLineJoin(context, kCGLineJoinRound);
	BOOL started = NO;
	//CGContextMoveToPoint(context, ChartLeftMargin, [self yCoordinate:[data objectAtIndex:0]]);
	
	for(int i = 0; i < [dataArray count] ; i++){
		SC_PointData *point = (SC_PointData *)[dataArray objectAtIndex:i];
		float x ;
		if (i == 0) {
			x = i * CurvePointDistance + ChartLeftMargin + 1.0f;
		}
		else {
			x = i * CurvePointDistance + ChartLeftMargin;
		}

		
		if(point.yValue != nil){
			
			float y = [self valueToYCoordinate:[point.yValue floatValue]];
			//NSLog(@"orgin: %.0f  Y: %.0f Back: %.0f",[point.yValue floatValue],y,[self yCoordinateToValue:y]);
			if(!started){
				CGContextMoveToPoint(context, x, y);
				started = YES;
			}else
				CGContextAddLineToPoint(context, x, y);
			
		}
	}
	CGContextStrokePath(context);
	CGContextRestoreGState(context);
}
- (void)drawCurveFill:(CGContextRef)context{
	
	if([dataArray count] < 2) return;
	
	
	CGContextSaveGState(context);
	
	CGContextSetRGBFillColor(context, (230.0 / 255.0), (242.0/255.0), (250.0 / 255.0), 0.9);
	CGContextSetLineWidth(context, 0);
	
	
	BOOL started = NO;
	
	int i=0,j=0;
	
	for(; i < [dataArray count]; i++){
		SC_PointData *point = (SC_PointData *)[dataArray objectAtIndex:i];
		float y = [self valueToYCoordinate:[point.yValue floatValue]];
		//NSLog(@"point y = %.0f",[point.yValue floatValue]);
		//NSLog(@"y value = %.0f",y);
		if(point.yValue != nil){
			if(!started){
				if (i == 0) {
					CGContextMoveToPoint(context, ChartLeftMargin + 1.0f, self.frame.size.height - ChartBottomMargin - 1.0f);

				}
				else {
					CGContextMoveToPoint(context, i * CurvePointDistance + ChartLeftMargin, self.frame.size.height - ChartBottomMargin);

				}
				started = YES;
			}
			if (i == 0) {
				CGContextAddLineToPoint(context, i * CurvePointDistance + 1.0f + ChartLeftMargin,y);
			}
			else {
				CGContextAddLineToPoint(context, i * CurvePointDistance + ChartLeftMargin,y);
			}

			
			j=i;
		}
	}
	
	CGContextAddLineToPoint(context,j * CurvePointDistance + ChartLeftMargin, self.frame.size.height - ChartBottomMargin - 1.0f);
	
	CGContextClosePath(context);
	CGContextDrawPath(context, kCGPathFillStroke);
	
	CGContextRestoreGState(context);
	
}
//- (void)drawIndicator:(CGContextRef )context {
//	float height = self.frame.size.height;
//	CGContextSetStrokeColorWithColor(context,[UIColor greenColor].CGColor);
//	CGContextSetLineWidth(context, 1.0);
//	CGPoint addLines[] =
//	{
//		CGPointMake(indicatorPoint.x, ChartTopMargin),
//		CGPointMake(indicatorPoint.x, height - ChartBottomMargin),
//	};
//	CGContextAddLines(context, addLines, sizeof(addLines)/sizeof(addLines[0]));
//	CGContextStrokePath(context);
//	
//	CGContextSetFillColorWithColor(context,[UIColor orangeColor].CGColor);
//	
//	CGContextSetLineWidth(context, 3);
//	CGContextFillEllipseInRect(context, CGRectMake(indicatorPoint.x-6, indicatorPoint.y-6, 12.0, 12.0));
//	
//	CGSize size = [indicatorValue sizeWithFont:[UIFont systemFontOfSize:12.0f]];
//	if (size.height < 15.0f) {
//		size.height = 15.0f;
//	}
//	if (size.width < 40.0f) {
//		size.width = 40.0f;
//	}
//	CGRect rect;
//	rect.origin.x = indicatorPoint.x - size.width / 2;
//	rect.origin.y = ChartTopMargin - size.height;
//	rect.size = size;
//	[UIView drawRoundRectangleInRect:rect withRadius:4.0 color:[UIColor grayColor]];
//	
//	[[UIColor whiteColor] set];
//	[indicatorValue drawInRect:rect withFont:[UIFont boldSystemFontOfSize:12.0f] lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
//	[[UIColor grayColor] set];
//	
//	CGSize labelSize = [indicatorXLabel sizeWithFont:[UIFont systemFontOfSize:12.0f]];
//	
//	float margin = 0.0f;
//	if (indicatorPoint.x > self.frame.size.width / 2) {
//		margin = labelSize.width;
//	}
//	[indicatorXLabel drawAtPoint:CGPointMake(indicatorPoint.x - margin, ChartTopMargin)//self.frame.size.height - ChartBottomMargin - labelSize.height)
//						withFont:[UIFont systemFontOfSize:12.0f]];
//}
- (void)drawStandardLine:(CGContextRef )context {
	float width = self.frame.size.width;
	CGContextSetStrokeColorWithColor(context,[UIColor redColor].CGColor);
	CGContextSetLineWidth(context, 1.0);
	CGPoint addLines[] =
	{
		CGPointMake(ChartLeftMargin, [self valueToYCoordinate:standardValue]),
		CGPointMake(width - ChartRightMargin, [self valueToYCoordinate:standardValue]),
	};
	CGContextAddLines(context, addLines, sizeof(addLines)/sizeof(addLines[0]));
	CGContextStrokePath(context);
	
	// draw standard label
	[[UIColor redColor] set];
	NSString *string;
	if (standardValue < 1.0f) {
		string = [NSString stringWithFormat:@"%.2f",standardValue];
	}
	else if (standardValue < 20.0f) {
		string = [NSString stringWithFormat:@"%.1f",standardValue];
	}
	else {
		string = [NSString stringWithFormat:@"%.0f",standardValue];
	}
	
	CGSize size = [string sizeWithFont:[UIFont systemFontOfSize:AxisLabelFontSize]];
	CGPoint point = CGPointMake(ChartLeftMargin - size.width - YAxisLineLength, [self valueToYCoordinate:standardValue] - size.height / 2);
	[string drawAtPoint:point withFont:[UIFont systemFontOfSize:AxisLabelFontSize]];
	
	
}
- (void)drawTitleText:(CGContextRef )context {
	[[UIColor blackColor] set];
	CGSize size = [title sizeWithFont:[UIFont systemFontOfSize:16.0f]];
	[title drawAtPoint:CGPointMake(240.0f - size.width / 2, 5.0f)
			  withFont:[UIFont systemFontOfSize:16.0f]];
}
- (void)drawLowLimitedLine:(CGContextRef)context {
	float width = self.frame.size.width;
	CGContextSetStrokeColorWithColor(context,[UIColor redColor].CGColor);
	CGContextSetLineWidth(context, 1.0);
	CGPoint addLines[] =
	{
		CGPointMake(ChartLeftMargin, [self valueToYCoordinate:lowLimitedValue]),
		CGPointMake(width - ChartRightMargin, [self valueToYCoordinate:lowLimitedValue]),
	};
	CGContextAddLines(context, addLines, sizeof(addLines)/sizeof(addLines[0]));
	CGContextStrokePath(context);
	
	// draw standard label
	[[UIColor redColor] set];
	
	NSString *string;
	if (lowLimitedValue < 1.0f) {
		string = [NSString stringWithFormat:@"%.2f",lowLimitedValue];
	}
	else if (lowLimitedValue < 20.0f) {
		string = [NSString stringWithFormat:@"%.1f",lowLimitedValue];
	}
	else {
		string = [NSString stringWithFormat:@"%.0f",lowLimitedValue];
	}
	
	CGSize size = [string sizeWithFont:[UIFont systemFontOfSize:AxisLabelFontSize]];
	CGPoint point = CGPointMake(ChartLeftMargin - size.width - YAxisLineLength, [self valueToYCoordinate:lowLimitedValue] - size.height / 2);
	[string drawAtPoint:point withFont:[UIFont systemFontOfSize:AxisLabelFontSize]];
}
#pragma mark Touch event
- (void)proccessForTouch:(UITouch *)touch {
	
	if ([dataArray count] < 2) {
		return;
	}
	
	CGPoint point = [touch locationInView:self];
	CGRect rect = CGRectMake(ChartLeftMargin, ChartTopMargin,
							 self.frame.size.width - ChartLeftMargin - ChartRightMargin,
							 self.frame.size.height - ChartTopMargin - ChartBottomMargin);
	if (CGRectContainsPoint(rect, point)) {
		int i = (point.x - ChartLeftMargin + CurvePointDistance / 2) / CurvePointDistance;
		[self showIndicatorForIndex:i];
	}
	
	
}

- (void)showIndicatorForIndex:(NSInteger )index {
	
	SC_PointData *point = [dataArray objectAtIndex:index];
	
	float x = CurvePointDistance * index + ChartLeftMargin;
	float y = [self valueToYCoordinate:[point.yValue floatValue]];
	indicatorView.indicatorPoint = CGPointMake(x, y);
	[indicatorView.indicatorValue setString:[NSString stringWithFormat:@"%.2f",[point.yValue floatValue]]];
	[indicatorView.indicatorXLabel setString:point.xLabel];
	
	[indicatorView setNeedsDisplay];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
		//NSLog(@"Touch began");
	indicatorView.needShowIndicator = YES;
	[self proccessForTouch:[touches anyObject]];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	//NSLog(@"Touch moved");
	indicatorView.needShowIndicator = YES;
	[self proccessForTouch:[touches anyObject]];
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
	//NSLog(@"Touch");
	indicatorView.needShowIndicator = NO;
	[indicatorView setNeedsDisplay];
	//[self setNeedsDisplay];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
	//NSLog(@"Touch cancelled");
}

#pragma mark For Standar Lines
- (void)setStandardValue:(float)value {
	standardValue = value;
	[self calculateHighValue];
	[self setNeedsDisplay];
}
- (void)setLowLimitedValue:(float )value {
	lowLimitedValue = value;
	[self setNeedsDisplay];
}

#pragma mark dealloc Methods
- (void)dealloc {
	[dataArray release];

    [super dealloc];
}

#pragma mark UTILITY FUNCTIONS
- (float) valueToYCoordinate:(float)value{
	float q = self.frame.size.height - ChartTopMargin - ChartBottomMargin;
	if (value == 0) {
		return q + ChartTopMargin;
	}
	q = q - ( q / DataHighValue * value) + ChartTopMargin;
	return q;
}
- (float) yCoordinateToValue:(float)y{
	float q = self.frame.size.height - ChartTopMargin - ChartBottomMargin;
	q = (q - y + ChartTopMargin) * DataHighValue / q;
	return q;
}

@end
